﻿/*
*		IP服务器，进行监听客户端连接
*		该服务器默认为本机IP，端口为6655端口,默认为4个最大客户端
*/


#pragma once


#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/signals2.hpp>
#include <vector>

#include "CxPort.h"


namespace ImitateTansfer
{
	using boost::noncopyable;
	using boost::asio::io_service;
	using boost::thread;
	using namespace boost::asio::ip;
	using namespace boost::asio;
	using std::vector;

	class	CxIPService	:	public	noncopyable
	{

		typedef boost::signals2::signal<void(size_t)> _Signal;

	public:

		CxIPService(LPCSTR portname,	vector<CxPort *>	*portgroup,
			UINT	portno=0,UINT	maxclient=4);
		~CxIPService();
	public:

		bool	OpenPort(LPCSTR portname);
		bool	ClosePort();
		bool	IsOpen()const;


		_Signal					sig_connected;



		//全局完成端口，整个程序仅此一个
		static	io_service		s_ioservice;
		//监视线程
		static	thread			*s_pwatchthread;
	private:

        static	void		Accept_Handle(CxIPService *_this,tcp::socket *psock,char *buffer
													,const boost::system::error_code &e);

        static	void		Accept_Handle_1(CxIPService *_this,tcp::socket *psock
													,const boost::system::error_code &e);

        static	void		Watch_Handle(CxIPService *_this);

        void                        Start();

	private:

		UINT					m_maxclient;
		address					m_addr;
		UINT					m_portno;
		tcp::acceptor			m_acceptor;
		vector<CxPort*>			*m_pportgroup;


	};


}
